/**
 * 
 */
package us.gaaoc.framework.webapp.backingbeans.judge;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.faces.component.UIParameter;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.hibernate.Hibernate;

import us.gaaoc.framework.model.CaseRecord;
import us.gaaoc.framework.model.CaseRecordStatus;
import us.gaaoc.framework.model.Documents;
import us.gaaoc.framework.model.LdapDirectoryPerson;
import us.gaaoc.framework.model.OrgParticipants;
import us.gaaoc.framework.model.Person;
import us.gaaoc.framework.model.PersonParticipants;
import us.gaaoc.framework.model.Signatures;
import us.gaaoc.framework.signature.digital.DigitalSignerController;
import us.gaaoc.framework.util.LocalCaseInformation;
import us.gaaoc.framework.util.io.FileUtility;
import us.gaaoc.framework.webapp.backingbeans.BaseBackingBean;

/**
 * @author Sukhada
 * Jul 6, 2006
 * GAJE_BibbCounty_1.0
 */
@SuppressWarnings("unchecked")
public class CaseDetailsPage extends BaseBackingBean implements Serializable {
	
	/**
	 * Properties
	 */

	/** Generated Serial Version UID */
	private static final long serialVersionUID = -2573764076494541557L;
	private Logger LOG = Logger.getLogger(CaseDetailsPage.class);
	
	private CaseRecord caseRecord;
	private String filerName;
	private Date courtDate;

	/**
	 * Data Models for the personParticipants
	 */
	private DataModel initiatingPartyPersonModel;
	private DataModel defendentPartyPersonModel;
	private DataModel witnessPersonModel;
	private DataModel plantiffsAttorneyModel;
	private DataModel defendentsAttorneyModel;
	private DataModel initiatingPartyOrganizationDataModel;
	private DataModel importedDocumentsModel;
	private DataModel uploadedDocumentsModel;
	private String caseCaption;
	private List<Documents> documentList;
	private List<Documents> importedDocumentsList;
	private List<Documents> uploadedDocumentsList;
	private boolean renderSignButton;
	private UIParameter signatureBinding;
	private boolean renderSignedText;
	private List<Signatures> signaturesList;
	private Documents signedDoc;
	/**
	 * Flag for the Send to Clerk button
	 */
	private boolean renderSendButton;
	/**
	 * Flag for the Reject Filing
	 */
	private boolean renderRejectButton;
	/**
	 * Name of the county
	 */
	private String jurisdiction;
	
	private  DigitalSignerController digitalSignerController = new DigitalSignerController();

	private String courtUuid;
	
	/**
	 * Constructor
	 *
	 */
	public CaseDetailsPage() {
		courtUuid = (String) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("courtUuid");
		this.loadData();
	}

	/**
	 * @return the rendernedText
	 */
	public boolean isRenderSignedText() {
		return renderSignedText;
	}

	/**
	 * @param renderSignedText the renderSignedText to set
	 */
	public void setRenderSignedText(boolean renderSignedText) {
		this.renderSignedText = renderSignedText;
	}

	/**
	 * Getter for signatureBinding
	 * @return signatureBinding
	 */
	public UIParameter getSignatureBinding() {
		return signatureBinding;
	}

	/**
	 * Setter for property signatureBinding
	 * @param signatureBinding
	 */
	public void setSignatureBinding(UIParameter signatureBinding) {
		this.signatureBinding = signatureBinding;
	}

	/**
	 * Getter for property renderSignButton
	 * @return renderSignButton
	 */
	public boolean isRenderSignButton() {
		return renderSignButton;
	}

	/**
	 * Setter for property renderSignButton
	 * @param renderSignButton
	 */
	public void setRenderSignButton(boolean renderSignButton) {
		this.renderSignButton = renderSignButton;
	}

	/**
	 * This method loads the data from the database
	 *
	 */
	private void loadData() {
		Map sessionMap = this.getFacesContext().getExternalContext()
				.getSessionMap();
		String courtUuid = (String) sessionMap.get("courtUuid");
		try {
			this.setJurisdiction(LocalCaseInformation.getJurisdiction(courtUuid));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		String caseUuid = (String) sessionMap.get("caseUuid");
		CaseRecord c = (CaseRecord) this.getModelFacade().findCaseRecordById(
				caseUuid, courtUuid);
		this.setCaseRecord(c);
		Person personSigned = (Person) this.getFacesContext()
				.getExternalContext().getSessionMap().get("person");
		
		// Initializing the objects which we are going to access in this method
		Hibernate.initialize(c.getPersonParticipants());
		Hibernate.initialize(c.getOrgParticipants());
		Hibernate.initialize(c.getDocuments());
		Hibernate.initialize(c.getStatus());
		Hibernate.initialize(c.getLocalCaseCategory());
		Hibernate.initialize(c.getLocalCaseDivision());
		Hibernate.initialize(c.getLocalCaseFilingType());
		Hibernate.initialize(c.getLocalCaseType());
		
		// b7006e6c-695c-11dd-945f-00301b45ff56
		
		for (int i=0; i< c.getPersonParticipants().size(); i++) {
			Hibernate.initialize(
					c.getPersonParticipants().get(i).getPerson().getAddresses()
					);
		}
		for (int i=0; i< c.getOrgParticipants().size(); i++) {
			Hibernate.initialize(
					c.getOrgParticipants().get(i).getOrganizations().getAddresses()
					);
		}
		for (int i = 0; i < c.getDocuments().size(); i++) {
			Hibernate.initialize(
					c.getDocuments().get(i).getDocumentInstance()
					);
		}
		
		/* */
		List<PersonParticipants> pParticipantsList = c.getPersonParticipants();
		List<OrgParticipants> oParticipantsList = c.getOrgParticipants();
		List initiatingPartyOrganizationList = new ArrayList();
		// for initiating party person list
		List initiatingPartyPersonList = new ArrayList();
		// for defendent party person list
		List defendentPartyPersonList = new ArrayList();
		// for witness person list
		List witnessPersonList = new ArrayList();
		// for plantiff's Attorneys
		List plantiffsAttorneyList = new ArrayList();
		// for defendent's Attorneys
		List defendentsAttorneyList = new ArrayList();
		for (OrgParticipants o : oParticipantsList) {
			if (o.getRelationship().getCode() == 1)
				initiatingPartyOrganizationList.add(o);
		}

		for (PersonParticipants o : pParticipantsList) {
			if (o.getRelationship().getCode() == 1)
				initiatingPartyPersonList.add(o);
			else if (o.getRelationship().getCode() == 2)
				defendentPartyPersonList.add(o);
			else if (o.getRelationship().getCode() == 3)
				witnessPersonList.add(o);
			else if (o.getRelationship().getCode() == 4 && !o.getPerson().getFirstName().equalsIgnoreCase("ff20da2c-8e5c-11dd-9b78-0016d3"))
				plantiffsAttorneyList.add(o);
			else if (o.getRelationship().getCode() == 5)
				defendentsAttorneyList.add(o);
			else if (o.getRelationship().getCode() == 6) {
				Person filer = o.getPerson();
				this.setFilerName(filer.getFirstName() + " "
						+ filer.getMiddleName() + " " + filer.getLastName());
			}
		}

		initiatingPartyOrganizationDataModel = new ListDataModel();
		initiatingPartyOrganizationDataModel
				.setWrappedData(initiatingPartyOrganizationList);

		initiatingPartyPersonModel = new ListDataModel();
		initiatingPartyPersonModel.setWrappedData(initiatingPartyPersonList);

		defendentPartyPersonModel = new ListDataModel();
		defendentPartyPersonModel.setWrappedData(defendentPartyPersonList);

		witnessPersonModel = new ListDataModel();
		witnessPersonModel.setWrappedData(witnessPersonList);

		defendentsAttorneyModel = new ListDataModel();
		defendentsAttorneyModel.setWrappedData(defendentsAttorneyList);

		plantiffsAttorneyModel = new ListDataModel();
		plantiffsAttorneyModel.setWrappedData(plantiffsAttorneyList);
		
		// check for the status of the caseRecord and depending on that 
		// render the send button which sends the filings to court
		int statusCode = c.getStatus().getCode();
		//if (caseStatus.equals("ReadyToDispose")) {
		if (statusCode == 17) {
			this.setRenderSendButton(true);
			this.setRenderRejectButton(false);
		} else {
			this.setRenderSendButton(false);
			this.setRenderRejectButton(true);
		}

		List<Documents> pdfList = new ArrayList<Documents>();
		List<Documents> iDocs = new ArrayList<Documents>();
		List<Documents> uDocs = new ArrayList<Documents>();

		boolean docOrder = false;
		boolean docSigned = false;

		pdfList = this.getModelFacade().getPDFDocuments(caseUuid);
		for(Documents document : pdfList) {
			if(document.getStatus().getCode() == 2) {
				iDocs.add(document);
			}
			else if(document.getStatus().getCode() == 6) {
				uDocs.add(document);
			}
		}
		for (Documents doc : pdfList) {
			docOrder = digitalSignerController.isDocumentOrderForJudge(doc);
			docSigned = digitalSignerController.isDocumentSignedForJudge(doc,personSigned);			
			if (docOrder) {
				if (docSigned) {
					this.renderSignedText = true;
					this.renderSignButton = false;
				} else {
					this.renderSignedText = false;
					this.renderSignButton = true;
				}
			}
		}
		
		for (Documents doc : uDocs) {
			docOrder = digitalSignerController.isDocumentOrderForJudge(doc);
			if (!docOrder) {
				this.setRenderSendButton(true);
			} else {
				this.setRenderSendButton(false);
				break;
			}
		}
		
		//caseStatus = "ReadyToDispose" for statusCode=17;
		//caseStatus = "RejectedByJudge" for statusCode=19;
		if (!((statusCode == 17) || (statusCode == 19)) ) {
			this.updateDatabaseAfterAllSigned(pdfList, personSigned);
		}
		
		if(pdfList.size()>0){
			this.setDocumentList(pdfList);
		}
		this.setImportedDocumentsList(iDocs);
		this.setUploadedDocumentsList(uDocs);

		importedDocumentsModel = new ListDataModel();
		uploadedDocumentsModel = new ListDataModel();
		
		importedDocumentsModel.setWrappedData(this.getImportedDocumentsList());
		uploadedDocumentsModel.setWrappedData(this.getUploadedDocumentsList());
		
		
		// set court date
		this.setCourtDate(this.getModelFacade().findCourtDate(
				this.getCaseRecord().getUuid()));
		// set case caption
		if (!initiatingPartyOrganizationList.isEmpty()) {
			String initiatingPartyOrgName = ((OrgParticipants) initiatingPartyOrganizationList
					.get(0)).getOrganizations().getName();
			if (!defendentPartyPersonList.isEmpty()) {
				Person defendent = ((PersonParticipants) defendentPartyPersonList
						.get(0)).getPerson();
				String defendentName = defendent.getFirstName() + " "
						+ defendent.getMiddleName() + " "
						+ defendent.getLastName();
				this.setCaseCaption(initiatingPartyOrgName + " vs "
						+ defendentName);
			}
		}
	}

	/**
	 * This method navigates user to the Import Filing Page
	 * @return String
	 */
	public String gotoImportFiling() {
		return "gotoImportFiling";
	}

	/**
	 * This method navigates user to the Reject Filing Page
	 * @return String
	 */
	public String gotoRejectFiling() {
		return "gotoRejectFiling";
	}

	/**
	 * This method navigates user to the previous page 
	 * @return String
	 */
	public String back() {
		Map sessionMap = this.getFacesContext().getExternalContext()
				.getSessionMap();
		String fromPage = (String) sessionMap.get("from");
		sessionMap.remove("from");
		sessionMap.remove("caseUuid");

		if (fromPage.equalsIgnoreCase("ChildEnforcementCaseTypePage")) {
			return "childEnforcement";
		} else if (fromPage.equalsIgnoreCase("ReceivedFilingPage")) {
			return "received";
		} else if (fromPage.equalsIgnoreCase("RejectedFilingPage")) {
			return "rejected";
		} else if (fromPage.equalsIgnoreCase("ImportedFilingPage")) {
			return "imported";
		} else if (fromPage.equalsIgnoreCase("DisposedFilingPage")) {
			return "disposed";
		} else if (fromPage.equalsIgnoreCase("TransmissionErrorFilingPage")) {
			return "transmissionError";
		} else if (fromPage.equalsIgnoreCase("ServedFilingPage")) {
			return "served";
		} else if (fromPage.equalsIgnoreCase("NonServedFilingPage")) {
			return "served";
		} else if (fromPage.equalsIgnoreCase("publicNewFilingPage")) {
			return "newFiling";
		} else if (fromPage.equalsIgnoreCase("AppellateCaseTypePage"))
			return "appellate";
		else if (fromPage.equalsIgnoreCase("CivilCaseTypePage"))
			return "civil";
		else if (fromPage.equalsIgnoreCase("childSupportTrashCanPage"))
			return "childSupportTrashCanPage";
		else if (fromPage.equalsIgnoreCase("MainPage"))
			return "gotoMainPage";
		else if (fromPage.equalsIgnoreCase("judgeMainPage"))
			return "gotoMainPage";
		else if (fromPage.equalsIgnoreCase("searchPage"))
		{
			sessionMap.put("from","judgeMainPage");
			return "searchPage";
		}
		else
			return "";

	}

	/**
	 * @return Returns the caseRecord.
	 */
	public CaseRecord getCaseRecord() {
		return caseRecord;
	}

	/**
	 * @param caseRecord The caseRecord to set.
	 */
	public void setCaseRecord(CaseRecord caseRecord) {
		this.caseRecord = caseRecord;
	}

	/**
	 * @return Returns the courtDate.
	 */
	public Date getCourtDate() {
		return courtDate;
	}

	/**
	 * @param courtDate The courtDate to set.
	 */
	public void setCourtDate(Date courtDate) {
		this.courtDate = courtDate;
	}

	/**
	 * @return the signaturesList
	 */
	public List<Signatures> getSignaturesList() {
		return signaturesList;
	}

	/**
	 * @param sets signaturesList
	 */
	public void setSignaturesList(List<Signatures> signaturesList) {
		this.signaturesList = signaturesList;
	}

	/**
	 * @return Returns a signed document
	 */
	public Documents getSignedDoc() {
		return signedDoc;
	}

	/**
	 * @param sets a signed document.
	 */
	public void setSignedDoc(Documents signedDoc) {
		this.signedDoc = signedDoc;
	}

	/**
	 * @return Returns the documentList.
	 */
	public List<Documents> getDocumentList() {
		return documentList;
	}

	/**
	 * @param sets the documentList.
	 */
	public void setDocumentList(List<Documents> documentList) {
		this.documentList = documentList;
	}

	/**
	 * @return Returns the filerName.
	 */
	public String getFilerName() {
		return filerName;
	}

	/**
	 * @param filerName The filerName to set.
	 */
	public void setFilerName(String filerName) {
		this.filerName = filerName;
	}

	/**
	 * @return Returns the caseCaption.
	 */
	public String getCaseCaption() {
		return caseCaption;
	}

	/**
	 * @param caseCaption The caseCaption to set.
	 */
	public void setCaseCaption(String caseCaption) {
		this.caseCaption = caseCaption;
	}

	/**
	 * @return Returns the initiatingPartyOrganizationDataModel.
	 */
	public DataModel getInitiatingPartyOrganizationDataModel() {
		return initiatingPartyOrganizationDataModel;
	}

	/**
	 * @param initiatingPartyOrganizationDataModel The initiatingPartyOrganizationDataModel to set.
	 */
	public void setInitiatingPartyOrganizationDataModel(
			DataModel initiatingPartyOrganizationDataModel) {
		this.initiatingPartyOrganizationDataModel = initiatingPartyOrganizationDataModel;
	}

	/**
	 * @return Returns the initiatingPartyPersonModel.
	 */
	public DataModel getInitiatingPartyPersonModel() {
		return initiatingPartyPersonModel;
	}

	/**
	 * @param initiatingPartyPersonModel The initiatingPartyPersonModel to set.
	 */
	public void setInitiatingPartyPersonModel(
			DataModel initiatingPartyPersonModel) {
		this.initiatingPartyPersonModel = initiatingPartyPersonModel;
	}

	/**
	 * @return Returns the defendentPartyPersonModel.
	 */
	public DataModel getDefendentPartyPersonModel() {
		return defendentPartyPersonModel;
	}

	/**
	 * @param defendentPartyPersonModel The defendentPartyPersonModel to set.
	 */
	public void setDefendentPartyPersonModel(DataModel defendentPartyPersonModel) {
		this.defendentPartyPersonModel = defendentPartyPersonModel;
	}

	/**
	 * @return Returns the defendentsAttorneyModel.
	 */
	public DataModel getDefendentsAttorneyModel() {
		return defendentsAttorneyModel;
	}

	/**
	 * @param defendentsAttorneyModel The defendentsAttorneyModel to set.
	 */
	public void setDefendentsAttorneyModel(DataModel defendentsAttorneyModel) {
		this.defendentsAttorneyModel = defendentsAttorneyModel;
	}

	/**
	 * @return Returns the plantiffsAttorneyModel.
	 */
	public DataModel getPlantiffsAttorneyModel() {
		return plantiffsAttorneyModel;
	}

	/**
	 * @param plantiffsAttorneyModel The plantiffsAttorneyModel to set.
	 */
	public void setPlantiffsAttorneyModel(DataModel plantiffsAttorneyModel) {
		this.plantiffsAttorneyModel = plantiffsAttorneyModel;
	}

	/**
	 * @return Returns the witnessPersonModel.
	 */
	public DataModel getWitnessPersonModel() {
		return witnessPersonModel;
	}

	/**
	 * @param witnessPersonModel The witnessPersonModel to set.
	 */
	public void setWitnessPersonModel(DataModel witnessPersonModel) {
		this.witnessPersonModel = witnessPersonModel;
	}

	/**
	 * This method is to view the PDF/XML Document
	 * @return
	 */
	public String viewFile() {
		Map requestMap = this.getFacesContext().getExternalContext()
				.getRequestParameterMap();
		HttpServletResponse resp = (HttpServletResponse) this.getFacesContext()
				.getExternalContext().getResponse();
		String docTitle = (String) requestMap.get("title");

		byte[] docContent = null;
		for (Documents doc : this.getDocumentList()) {
			if (doc.getTitle().equalsIgnoreCase(docTitle)) {
				docContent = doc.getContent();
				break;
			}
		}
		try {
			FileUtility.streamReportToBrowser(resp, docTitle, docContent);
		} catch (IOException e) {
			LOG.error("Error in CaseDetailPage.viewFile() method: " + e);
		}
		this.getFacesContext().responseComplete();
		return "stay";
	}

	/**
	 * @return the jurisdiction
	 */
	public String getJurisdiction() {
		return jurisdiction;
	}

	/**
	 * @param jurisdiction the jurisdiction to set
	 */
	public void setJurisdiction(String jurisdiction) {
		this.jurisdiction = jurisdiction;
	}

	/**
	 * @return the renderSendButton
	 */
	public boolean isRenderSendButton() {
		return renderSendButton;
	}

	/**
	 * @param renderSendButton the renderSendButton to set
	 */
	public void setRenderSendButton(boolean renderSendButton) {
		this.renderSendButton = renderSendButton;
	}

	/**
	 * @return the renderRejectButton
	 */
	public boolean isRenderRejectButton() {
		return renderRejectButton;
	}

	/**
	 * @param renderRejectButton the renderRejectButton to set
	 */
	public void setRenderRejectButton(boolean renderRejectButton) {
		this.renderRejectButton = renderRejectButton;
	}

	/**
	 * @return the importedDocumentsList
	 */
	public List<Documents> getImportedDocumentsList() {
		return importedDocumentsList;
	}

	/**
	 * @param importedDocumentsList the importedDocumentsList to set
	 */
	public void setImportedDocumentsList(List<Documents> importedDocumentsList) {
		this.importedDocumentsList = importedDocumentsList;
	}

	/**
	 * @return the importedDocumentsModel
	 */
	public DataModel getImportedDocumentsModel() {
		return importedDocumentsModel;
	}

	/**
	 * @param importedDocumentsModel the importedDocumentsModel to set
	 */
	public void setImportedDocumentsModel(DataModel importedDocumentsModel) {
		this.importedDocumentsModel = importedDocumentsModel;
	}

	/**
	 * @return the uploadedDocumentsList
	 */
	public List<Documents> getUploadedDocumentsList() {
		return uploadedDocumentsList;
	}

	/**
	 * @param uploadedDocumentsList the uploadedDocumentsList to set
	 */
	public void setUploadedDocumentsList(List<Documents> uploadedDocumentsList) {
		this.uploadedDocumentsList = uploadedDocumentsList;
	}

	/**
	 * @return the uploadedDocumentsModel
	 */
	public DataModel getUploadedDocumentsModel() {
		return uploadedDocumentsModel;
	}

	/**
	 * @param uploadedDocumentsModel the uploadedDocumentsModel to set
	 */
	public void setUploadedDocumentsModel(DataModel uploadedDocumentsModel) {
		this.uploadedDocumentsModel = uploadedDocumentsModel;
	}

	/**
	 * This method is get called when user press the sign button to sign
	 * PDF or XML document
	 * @return String
	 * @throws Exception
	 */
	public String signDocument() throws Exception {
		Documents doc = (Documents) this.getSignatureBinding().getValue();
		Person personSigner = (Person) this.getFacesContext()
				.getExternalContext().getSessionMap().get("person");
		/*
		 * Retrieve the certificate password
		 */
		LdapDirectoryPerson ldPerson = this.getModelFacade().findLdapDirectoryByPerson(personSigner);
	
		DigitalSignerController dsc = new DigitalSignerController();
		String judgeRoleUuid = "uuid4"; // The Judge Role UUID
		//dsc.signDocument(doc, personSigner,judgeRoleUuid,ldPerson);
		dsc.signDocument(doc, personSigner,judgeRoleUuid);
		// update the signed field in the documents object
		doc.setSignedDoc(dsc.isSignedDoc());
		// for rendering purpose in the xhtml page set the field ordered as
		// false
		doc.setOrderedDoc(dsc.isOrderedDoc());
		
		return "stayWithRefresh";
	}

	/**
	 * This method is used to view the details of the Signature for e.g.
	 * the title of the document, name of the person signed the document 
	 * and which date the document signed.
	 * @return
	 * @throws Exception
	 */
	public String viewSignatureDetails() throws Exception {
		LOG.info("From viewSignatureConfirmationPage to vewSignatureDetails");
		Documents doc = (Documents) this.getSignatureBinding().getValue();
		if (doc.getPhysicalType().equalsIgnoreCase("pdf")) {

			this.signaturesList = doc.getSignatures();

			this.signedDoc = doc;
			this.caseRecord = doc.getCaseRecord();
		}
		return "viewPdfSignatureDetails";
	}

	/**
	 * This method sends the case to the Clerk and navigates to the main Page of the Judge.
	 * @return {@link String}
	 */
	public String sendToClerk() {
		Map sessionMap = this.getFacesContext().getExternalContext()
				.getSessionMap();
		String fromPage = (String) sessionMap.get("from");
		Person person = (Person)sessionMap.get("person");
		
		this.getModelFacade().updateCaseRecordStatus(this.getCaseRecord().getUuid(),
				new CaseRecordStatus(18, "ReceivedFromJudge"), courtUuid);
		this.getModelFacade().addCaseRecordHistory(this.getCaseRecord().getUuid(), 14, person, courtUuid,"no comments");
		
		// b7006e6c-695c-11dd-945f-00301b45ff56
		// commit the transaction
		//HibernatePhaseListener.getSessionFactory().getCurrentSession()
		//		.getTransaction().commit();
		/*
		try {
			this.sendEmailNotification();
		} catch (Exception e) {
			e.printStackTrace();
		}		
		*/
		
		sessionMap.remove("from");
		sessionMap.remove("caseUuid");
		if (fromPage.equalsIgnoreCase("ChildEnforcementCaseTypePage")) 
			return "childEnforcement";
		else if (fromPage.equalsIgnoreCase("MainPage"))
			return "gotoMainPage";
		else
			return "";
	}
	
	/**
	 * This method navigates user to the Reject confirmation page for Judge to reject
	 * @return {@link String}
	 */
	public String rejectFiling () {
		Map sessionMap = this.getFacesContext().getExternalContext().getSessionMap();
		sessionMap.put("judgeCaseDetailsPage",this);		
		return "gotoRejectFiling";
	}
	
	/**
	 * This method updates the database after all documents signed by Judge
	 * @param docs
	 * @param person
	 */
	private void updateDatabaseAfterAllSigned(List<Documents> docs,
			Person person) {
		if (this.isAllDocumentsSigned(docs, person)) {
			this.getModelFacade().updateCaseRecordStatus(
					this.getCaseRecord().getUuid(),
					new CaseRecordStatus(17, "ReadyToDispose"), courtUuid);
			// remove the Modify Button from the Page
			//this.setRenderModifyButton(false);
			// Put the Submit Button on the Page to Submit the Filing to Court
			this.setRenderSendButton(true);
		}
	
	}
	
	/**
	 * This method checks whether all documents (which should be signed by Judge) are
	 * signed by judge or not. 
	 * @param docs
	 * @param person
	 * @return {@link Boolean}
	 */
	private boolean isAllDocumentsSigned(List<Documents> docs, Person person) {
		boolean documentsSigned = false;
		boolean ordered = false;
		List<Documents> orderedDocs = new ArrayList<Documents>();
		for (Documents d : docs) {
			ordered = digitalSignerController.isDocumentOrderForJudge(d);
			if (ordered) {
				orderedDocs.add(d);
			}
		}		
		List<Signatures> docSignatures = new ArrayList<Signatures>();
		if(! orderedDocs.isEmpty()) {
			for (Documents oDoc : orderedDocs) {
				docSignatures = oDoc.getSignatures();
				documentsSigned = false;
				for (Signatures s : docSignatures) {
					Person personSigned = s.getSignedByPerson();
					System.out.println("Signed Person Uuid "
							+ personSigned.getUuid());
					System.out.println("Parameter passes Person Uuid "
							+ person.getUuid());
					if (personSigned.getUuid().equals(person.getUuid())) {
						documentsSigned = true;
						break;
					}
					//break;
				}
				if (!documentsSigned)
					break;
			}
		}else {
			documentsSigned = true;
		}
		return documentsSigned;
	}
}
